<?php
/**
 * Created by PhpStorm.
 * User: sky
 * Date: 2019/7/16
 * Time: 23:04
 */

namespace App\Repositories;

use App\Models\Category;

class CategoryRepositories
{
    public function model()
    {
        return Category::query();
    }

    public function find($id)
    {
        return $this->model()->where('id',$id)->first();
    }

    public function save($data)
    {
        if(isset($data['status']) && $data['status'] == 'on')
        {
            $data['status'] = 1;
        }
        else{
            $data['status'] = 0;
        }
        return $this->model()->create($data);
    }
    public function update($id,$data)
    {
        return $this->model()->where('id',$id)->update($data);
    }

    public function search($data)
    {
        $model = $this->model();

        if(isset($data['title']))
        {
            $model = $model->where('title','like','%'.$data['title'].'%');
        }

        $data['limit'] = isset($data['limit'])? $data['limit']:20;

        //些处扩展更多查询条件
        return $model->orderBy('created_at','desc')->paginate($data['limit'])->toArray();
    }

    public function delete($ids)
    {
        return Category::destroy($ids);
    }

    public function allCategory($type = 'post')
    {
        return tree_category($this->model()->where('type',$type)->get()->toArray());
    }

}